//
//  HRGoodsParameterViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-8-16.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRGoodsParameterViewController.h"

#import "WebViewJavascriptBridge.h"

#define ANIMATION_DURATION 0.25f


#define WEB_METHOD_GOODS_PARAM @"clickAddSpCarAndBuy"

#define WEB_METHOD_SCHEDULED_GOODS_PARAM @"clickProductClassify"


@interface HRGoodsParameterViewController ()
@property(nonatomic,strong) WebViewJavascriptBridge *bridge;
@property (weak, nonatomic) IBOutlet UIView *coverView;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation HRGoodsParameterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //解决布局问题
//    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
//        self.edgesForExtendedLayout=UIRectEdgeNone;
//    }
    
    self.view.backgroundColor=[UIColor clearColor];

    [self initWebJavscriptBridge];

    
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideGoodsParam)];
    [self.coverView addGestureRecognizer:tapGesture];
  
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化构建Bridge控件
-(void)initWebJavscriptBridge{
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:nil];
    
    //获取商品参数
    [_bridge registerHandler:WEB_METHOD_GOODS_PARAM handler:^(id data, WVJBResponseCallback responseCallback) {
        [self goodsParamFromWebview:data];
    }];
    
    //获取预定商品的参数
    [_bridge registerHandler:WEB_METHOD_SCHEDULED_GOODS_PARAM handler:^(id data, WVJBResponseCallback responseCallback) {
        [self goodsParamFromWebview:data];
    }];
    
}

//加载测试html页面
- (void)loadExamplePage:(UIWebView*)webView {
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"chooseGoodsParam" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
    [_webView loadHTMLString:appHtml baseURL:baseURL];
}


#pragma mark - Methods

//显示“商品参数”页面
-(void)showGoodsParamInView:(UIView *)view{
    
    CGRect frame=self.view.frame;
    self.view.frame=CGRectMake(0, kScreenHeight, frame.size.width, frame.size.height);
    [view addSubview:self.view];
    
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        self.view.frame=CGRectMake(0, 0, frame.size.width, frame.size.height);
    } completion:^(BOOL finished) {
        NSString *urlStr=URL_WAP_GOODS_PARAMS(_goodsId);
        NSURL *url=[NSURL URLWithString:urlStr];
        
        [self showLoadingHud:kLoading];
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:url] progress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        } success:^NSString *(NSHTTPURLResponse *response, NSString *HTML) {
            [self hideLoadingHud];
            return HTML;
        } failure:^(NSError *error) {
            [self hideLoadingHud];
        }];
        
    }];
    
}

//隐藏“商品参数"
-(void)hideGoodsParam{
    
    CGRect frame=self.view.frame;
    
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        self.view.frame=CGRectMake(0, kScreenHeight, frame.size.width, frame.size.height);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

//从webview中获取商品的参数
-(void)goodsParamFromWebview:(NSString *)param{
    
    if ([_delegate respondsToSelector:@selector(buyGoodsSetParam:)]) {
        [_delegate buyGoodsSetParam:param];
    }
    
    [self hideGoodsParam];
}

//点击关闭按钮
- (IBAction)onCloseButtonActive:(id)sender {
    
    [self hideGoodsParam];
    
}

@end
